# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:   smart-water-iot-test
# FileName:     read_json
# Author:
# Datetime:    2022/4/6 14:29
# Description：
# -----------------------------------------------------------------------------------
import json
from common.util.configUtil.configUtil import ConfigUtil


class JsonUtil:
    def __init__(self):
        self.config = ConfigUtil()

    def read_sql(self, sql_key) -> str:
        """
                    ;True：线上环境
                    ;False：测试环境
                """
        sqlPath = self.config.get_sql_json_path()
        with open(sqlPath, encoding="utf-8") as data:
            sqlData = json.load(data)
            if self.config.get_database_status() is True:
                return sqlData["Pro"][sql_key]
            else:
                return sqlData["Test"][sql_key]

    def read_connack(self, code) -> str:
        code = str(code)
        connackPath = self.config.get_connackData_path()
        with open(connackPath, encoding="utf8") as data:
            jsonData = json.load(data)
            listK = []
            for k, v in jsonData.items():
                listK.append(k)
            if code in listK:
                return jsonData[code]

    # def read_equipmen(self, key="deviceName", value=1, mode="r"):
    #     """
    #     :param key: key值
    #     :param value:value值
    #     :param mode: 读写方式
    #     :return:
    #     1 、mode = r 时,仅读取json文件中的内容
    #     2 、mode = w 时,将组合有功参数写入json中
    #     """
    #     equipmenPath = self.file_path.get_Equipment_Data_path()
    #     if mode == "r":
    #         with open(equipmenPath, encoding="utf8") as data_reported:
    #             return json.load(data_reported)
    #     elif mode == "w":
    #         with open(equipmenPath, encoding="utf8") as data:
    #             readData = json.load(data)
    #         if key == "deviceName":
    #             readData[key] = value
    #             with open(equipmenPath, "w") as data_reported:
    #                 json.dump(readData, data_reported)
    #         else:
    #             readData["msg"][key] = str(value)
    #             with open(equipmenPath, "w") as data_reported:
    #                 json.dump(readData, data_reported)


if __name__ == '__main__':
    read_json = JsonUtil()
    print(read_json.read_sql("select_random_payamount"))
